package com.rashith.metro.alfa.webservices;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rashith.metro.alfa.modles.WebPlace;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class MyWebCallingClass extends AsyncTask<String, Integer, List<WebPlace>> {

	
	Context context;
	private double lat;

	ProgressDialog pDialog;
	private String cat;
	private double lon;
	private float rad;

	
	private MyWebCallInterface callback;

	public MyWebCallingClass(Context c, double latlatitude, double longitude,
			String _Category, float radiuos ,  MyWebCallInterface _callback) {
		context = c;
		lat = latlatitude;
		lon = longitude;
		cat = _Category;
		rad = radiuos;
		callback = _callback;
		pDialog = new ProgressDialog(context);
		pDialog.setMessage("Loading Places...");
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.setCancelable(true);
	}

	@Override
	protected void onPreExecute() {
		pDialog.setMessage("Loading Places...");
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pDialog.show();
	}

	@Override
	protected List<WebPlace> doInBackground(String... params) {
		
		String response = WebCall.getPlaces(lat, lon, cat, rad);

		if( response == null){
			return null;
			
		}
		String s = response.replace("</string>", "");
		s = s.replace("<string xmlns=\"http://www.metro360.com/\">", "");
		s = s.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");

		Gson parser = new Gson();

		Type myType = new TypeToken<List<WebPlace>>() {
		}.getType();
		List<WebPlace> list = parser.fromJson(s, myType);

		

		return list;
	}

	@Override
	protected void onPostExecute(List<WebPlace> result) {
		pDialog.dismiss();
		callback.doWork(result);
	}
	
}
